package com.intelligent.health.setmeal.controller;

import com.intelligent.health.setmeal.dto.SetMealDto;
import com.intelligent.health.setmeal.pojo.CheckGroup;
import com.intelligent.health.setmeal.service.SetMealService;
import com.intelligent.health.setmeal.pojo.SetMeal;
import com.intelligent.health.setmeal.vo.SetMealVo;
import entity.PageResult;
import entity.R;
import entity.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:35
 * @Description: 套餐
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 套餐的分页查询
     *
     * @param setMealDto 查询条件
     * @param pageNum 页码
     * @param pageSize 页页面大小
     * @return 套餐信息集合
     */
    @PostMapping("/all/{pageNum}/{pageSize}")
    public R querySetMealsByPage(@RequestBody SetMealDto setMealDto,
                                 @PathVariable("pageNum") Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize) {
        PageResult<SetMealVo> setMeals = setMealService.querySetMealsByPage(setMealDto, pageNum, pageSize);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("setMeals",setMeals);
    }

    /**
     * 根据setMealId查询setMeal
     *
     * @param setMealId 套餐id
     * @return 套餐信息
     */
    @GetMapping("/{setMealId}")
    public R querySetMealById(@PathVariable("setMealId") Integer setMealId) {
        SetMeal setMeal = setMealService.querySetMealById(setMealId);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("setMeal",setMeal);
    }

    /**
     * 添加套餐
     *
     * @param hashMap key:
     *                套餐:setMeal
     *                检查组Ids: checkGroupIds
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('SETMEAL_ADD')")
    @PostMapping("/add")
    public R addSetMeal(@RequestBody HashMap<String, Object> hashMap) {
        try {
            setMealService.addSetMeal(hashMap);
        } catch (Exception e) {
            log.error("添加套餐失败: {}", hashMap);
            return R.setResult(ResultCodeEnum.INSERT_ERROR);
        }
        log.info("添加套餐成功: {}", hashMap);
        return R.setResult(ResultCodeEnum.INSERT_SUCCESS);
    }

    /**
     * 编辑套餐
     *
     * @param hashMap key:
     *                套餐:setMeal
     *                检查组Ids: checkGroupIds
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('SETMEAL_EDIT')")
    @PutMapping("/edit")
    public R editSetMeal(@RequestBody HashMap<String, Object> hashMap) {
        try {
            setMealService.editSetMeal(hashMap);
        } catch (Exception e) {
            log.error("编辑套餐失败: {}", hashMap);
            return R.setResult(ResultCodeEnum.UPDATE_ERROR);
        }
        log.info("编辑套餐成功: {}", hashMap);
        return R.setResult(ResultCodeEnum.UPDATE_SUCCESS);
    }

    /**
     * 逻辑删除套餐
     *
     * @param setMealId 套餐id
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('SETMEAL_DELETE')")
    @DeleteMapping("/del/{setMealId}")
    public R delSetMeal(@PathVariable("setMealId") Integer setMealId) {
        try {
            setMealService.delSetMeal(setMealId);
        } catch (Exception e) {
            log.error("删除套餐失败: {}", setMealId);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("删除套餐成功: {}", setMealId);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 批量逻辑删除
     *
     * @param setMealIds 套餐ids
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('SETMEAL_DELETE')")
    @DeleteMapping("/delMore")
    public R delMoreSetMeal(@RequestBody ArrayList<Integer> setMealIds) {
        try {
            setMealService.delBatchSetMeal(setMealIds);
        } catch (Exception e) {
            log.error("批量删除套餐失败: {}", setMealIds);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("批量删除套餐成功: {}", setMealIds);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 查询套餐数据 for export Excel
     *
     * @param hashMap key: setMealDto value: SetMealDto(必须)
     *                key: pageInfo value: PageInfo(非必须)
     * @return
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasAuthority('SETMEAL_DELETE')")
    @PostMapping("/all/excel")
    public R querySetMeals(@RequestBody HashMap<String, Object> hashMap) {
        if (hashMap.get("setMealDto") == null) {
            return R.setResult(ResultCodeEnum.QUERY_ERROR);
        }
        List<SetMeal> setMeals = setMealService.querySetMeals(hashMap);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("setMeals",setMeals);
    }

    /**
     * 根据setMealId查询对应检查组
     * @param setMealId 套餐id
     * @return 检查组集合
     */
    @GetMapping("/checkGroups/{setMealId}")
    public R queryCheckGroupsBySid(@PathVariable("setMealId")Integer setMealId){
        List<CheckGroup> checkGroups = setMealService.queryCheckGroupsBySid(setMealId);
        return R.setResult(ResultCodeEnum.QUERY_SUCCESS).data("checkGroups",checkGroups);
    }
}
